package org.ly817.busCode.core.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author LuoYu
 * @date 2019/02/19 10:13
 * <p>
 * Description:
 * 最简原型服务端
 */
public class DemoServer {
    public static void main(String[] args) {
        // 接收+分发连接
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // 处理连接
        EventLoopGroup workGroup = new NioEventLoopGroup();

        try {
            // 启动服务的工具类 可以看做是构建配置的引导类
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new DemoInitializer());

            // 绑定端口 sync为启动动作
            Channel channel = bootstrap.bind(6666).sync().channel();
            channel.closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}
